package com.lab.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.CollegeDao;
import com.lab.dao.CourseDao;
import com.lab.dao.MajorDao;
import com.lab.dao.PassWordDao;
import com.lab.dao.ScoreDao;
import com.lab.dao.StudentDao;
import com.lab.dao.impl.CollegeDaoImpl;
import com.lab.dao.impl.CourseDaoImpl;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.dao.impl.PassWordDaoImpl;
import com.lab.dao.impl.ScoreDaoImpl;
import com.lab.dao.impl.StudentDaoImpl;
import com.lab.po.Major;
import com.lab.po.Student;

//控制层 -添加 逻辑处理
@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = (String) request.getParameter("method");
		if ("addStudent".equals(method)) {
			addStudent(request, response);
		}else if ("addColMaj".equals(method)) {
			addColMaj(request, response);
		}else if ("addCourse".equals(method)) {
			addCourse(request, response);
		}else if ("addScore".equals(method)) {
			addScore(request, response);
		}
		

	}

	private void addScore(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		int cou_id=Integer.parseInt(request.getParameter("cou_id"));
		int stId=Integer.parseInt(request.getSession().getAttribute("stId").toString());
		ScoreDao sd=new ScoreDaoImpl();
		Map<String,Integer> map=new HashMap<String,Integer>();
		map.put("stId", stId);
		map.put("cou_id", cou_id);
		int i=sd.choiceCourse(map);
		if(i!=0){
			System.out.println("添加成功！");
			request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
		}else{
			System.out.println("添加失败！");
			request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
		}
	}

	private void addCourse(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String cou_name=request.getParameter("course");
		CourseDao cd=new CourseDaoImpl();
		int i=cd.addCourse(cou_name);
		if(i!=0){
			System.out.println("添加成功！");
			request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
		}else{
			System.out.println("添加失败！");
			request.getRequestDispatcher("showAllServlet?method=showCourse").forward(request,response);
		}
	}

	private void addStudent(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		StudentDao studao = new StudentDaoImpl();
		Date date=new Date();
		int year=date.getYear()+1900;
		String y1=String.valueOf(year).substring(2);
		
		
		String stName = request.getParameter("stName");
		String stSex = request.getParameter("stSex");
		String stAge = request.getParameter("stAge");
		String stTel = request.getParameter("stTel");
		int maj_id = Integer.parseInt(request.getParameter("major"));
		System.out.println(maj_id);
		String stAddress = request.getParameter("stAddress");
		
		MajorDao majordao=new MajorDaoImpl();
		Major major=majordao.queryMajorByMaj_id(maj_id);
		String col_id=String.valueOf(major.getCol_id());
		
		if(col_id.length()==1){
			col_id="0"+col_id;
		}
		int stId=studao.createstId(maj_id);
		if(stId==0){
			stId=Integer.parseInt(y1+col_id+maj_id+"001");
		}else{
			stId=stId+1;
		}
		Student stu = new Student(stId,stName, stSex, stAge, stTel, maj_id,
				stAddress);
		
		boolean flag = studao.addStudent(stu);
		PassWordDao pd=new PassWordDaoImpl();
		Map map2=new HashMap();
		map2.put("stId", stId);
		map2.put("pwd", 123456);
		pd.addStuPwd(map2);
		if (flag && !(stTel == null) && !"".equals(stTel)) {
			request.setAttribute("msg", "添加成功!!");
			request.getRequestDispatcher("main.jsp").forward(request,response);
		} else {
			request.getRequestDispatcher("main.jsp").forward(request,response);
			System.out.println("添加失败!!");
		}
		
	}

	private void addColMaj(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String col_name=request.getParameter("college");
		String box=request.getParameter("box");
		String major=request.getParameter("major");
		
		CollegeDao collegedao=new CollegeDaoImpl();
		MajorDao majordao=new MajorDaoImpl();
		int result=collegedao.queryCollegeByName(box);
		int col_id=0;
		if(result==0){
			col_id=collegedao.queryMaxCol_id();
		}else{
			col_id=collegedao.queryCol_id(col_name);
		}

		int flage=0;
		if(result==0 && box!=null && !"".equals(box)){
			Map map=new HashMap();
			map.put("col_id", col_id+1);
			map.put("col_name", box);
			flage=collegedao.addCollege(map);
		}
		
		int result2=majordao.queryMajorByName(major);
		int flage2=0;
		if(result2==0 && major!=null && !"".equals(major)){
			int maj_id=majordao.queryMaxMaj_id();
			Map map=new HashMap();
			map.put("maj_id", maj_id);
			map.put("maj_name", major);
			if(flage==0){
				map.put("col_id", col_id);
			}else{
				map.put("col_id", col_id+1);
			}
			
			flage2=majordao.addMajor(map);
		}
		
		if (flage!=0 || flage2!=0) {
			String mainPage="common/success.jsp";
			request.setAttribute("mainPage",mainPage);
			System.out.println("添加成功!!");
			request.getRequestDispatcher("main.jsp").forward(request,response);
		}else{
			String mainPage="common/fail.jsp";
			request.setAttribute("mainPage", mainPage);
			request.getRequestDispatcher("main.jsp").forward(request,response);
			System.out.println("添加失败!!");
		}
	}

}
